home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 342_01.zip / DIOTST02.C < prev    next >
C/C++ Source or Header  |  1993-04-03  |  8KB  |  248 lines

  1. /*-
  2.  *  ----------------------------------------------------------------------
  3.  *  File        :   DIOTST02.C
  4.  *  Creator     :   Blake Miller
  5.  *  Version     :   01.01.00        February 1991
  6.  *  Language    :   Microsoft C     Version 5.1
  7.  *  Purpose     :   Intel 8255 Compatible Digital IO Functions
  8.  *              :   Test Program #2
  9.  *  ----------------------------------------------------------------------
  10.  *  Link : DIOLIB?.LIB
  11.  *  ----------------------------------------------------------------------
  12.  *  Revision History:
  13.  *  022891 BVM  :   Creation
  14.  *  ----------------------------------------------------------------------
  15.  */
  16.  
  17. #define P_NAME  "DIOTST02"
  18. #define P_TITL  "DIOLIB Multi Digital IO"
  19. #define P_AUTH  "Blake Miller"
  20. #define P_VERS  "01.01.00"
  21. #define P_DATE  "February 28, 1991"
  22. #define P_PURP  "Toggle bits on the 8255 ports"
  23.  
  24. /*  Include Files ----------------------------*/
  25.  
  26. #define     DIOTST02_C_DEFINED  1
  27. #include    <conio.h>
  28. #include    <stdio.h>
  29. #include    <stdlib.h>
  30. #include    "DIOLIB.H"
  31. #undef      DIOTST02_C_DEFINED
  32.  
  33. /*  Function Prototype -----------------------*/
  34.  
  35. void pg_exit (short);
  36. void kb_dump (void);    /* keyboard flush function  */
  37. void kb_wait (void);    /* keypress wait / message  */
  38.  
  39. /*-
  40.  *  ----------------------------------------------------------------------
  41.  *  Function Definition
  42.  *  ----------------------------------------------------------------------
  43.  */
  44.  
  45. void main (void)
  46.     {
  47.     short           i, j;       /* counter      */
  48.     short           bitnum;     /* bit number   */
  49.     short           bstate;     /* bit state    */
  50.     char            s[32];      /* gets buffer  */
  51.     DIODAT          dioprt;     /* port data    */
  52.  
  53.     short           chpcnt = 0; /* number 8255 on board */
  54.     short           bitcnt = 0; /* total number bits    */
  55.  
  56.     /*  Print Program Header
  57.      */
  58.     printf ("\n" );
  59.     printf ("Program    : %s - %s.\n", P_NAME, P_TITL );
  60.     printf ("Author     : %s\n", P_AUTH );
  61.     printf ("Version    : %s  %s\n", P_VERS, P_DATE );
  62.     printf ("Purpose    : %s.\n", P_PURP );
  63.     printf ("\n" );
  64.     printf ("This program will fiddle with the bits of the 8255.\n");
  65.     printf ("This program was designed to work with digital I/O\n");
  66.     printf ("boards containing multiple 8255s.\n");
  67.     printf ("This program assumes that the 8255s can do input and output!\n");
  68.     printf ("\n");
  69.     printf ("Do you wish to continue [Y]:N : ");
  70.     gets (s); if ( *s == 'N' ) exit (0);
  71.  
  72.     /*  Prompt user for number of 8255 on board.
  73.      *  Exit if an empty string is entered.
  74.      */
  75.     printf ("Enter the number of 8255s on the board : ");
  76.     gets (s); if ( *s == 0 ) exit (0);
  77.     chpcnt = atoi(s);
  78.     bitcnt = 24 * chpcnt;   /* 24 bits on each 8255 */
  79.  
  80.     /*  Set up 8255 DIODAT array size data
  81.      *  based on number of ports specified.
  82.      */
  83.     if ( dio_pa_aloc (chpcnt) ){
  84.         printf ("Error allocating memory for DIODAT buffers.\n");
  85.         exit (1);
  86.         }
  87.  
  88.     /*  Prompt user for base address of each 8255.
  89.      *  Exit if any empty string is entered.
  90.      */
  91.     printf ("NOTE: 300 Hex is 768 in decimal.\n");
  92.     printf ("NOTE: On most boards, the 8255s are spaced 4 bytes apart.\n");
  93.     for ( j = 0; j < chpcnt; j++ ){
  94.         printf ("Enter base address (decimal) of 8255 #%2d : ", j);
  95.         gets (s); if ( *s == 0 ) pg_exit (0);
  96.         i = atoi(s);            /* get address of 8255  */
  97.         dio_pa_setadr (j, i);   /* set address of 8255  */
  98.         }
  99.  
  100.     /*  --------------------------------------------------------
  101.      *  Digital Output Demonstration
  102.      *  --------------------------------------------------------
  103.      */
  104.  
  105.     printf ("\n" );
  106.     printf ("----------------------------------------------\n");
  107.     printf ("Digital Output Demonstration.\n");
  108.     printf ("Set up output detection circuitry on ANY of the 8255\n");
  109.     printf ("output lines.  They will be toggled, flashed, etc.\n");
  110.     printf ("ALL lines will be set for output so remove any driving\n");
  111.     printf ("(INPUT) circuitry at this time, please.\n");
  112.     kb_wait();
  113.  
  114.     for ( j = 0; j < chpcnt; j ++ ){
  115.         dio_pa_config (j, 0, 0, 0, 0);   /* all ports output     */
  116.         }
  117.  
  118.     printf ("Clear all bits in digital IO...\n");
  119.     for ( bitnum = 0; bitnum < bitcnt; bitnum++ ){
  120.         dio_pa_bitput (bitnum, 0);
  121.         }
  122.  
  123.     printf ("Toggle bits in digital IO.\n");
  124.     printf ("Press key to make it stop.\n");
  125.  
  126.     bitnum = 0; /* start at bit 0   */
  127.  
  128.     while ( 1 ){
  129.  
  130.         if ( kbhit() ) break;
  131.  
  132.         dio_pa_bitput (bitnum, 0);      /* clear bit (OFF)      */
  133.         bitnum++;                       /* next bit number      */
  134.         bitnum = (bitnum % bitcnt);     /* Range: [0:bitnum-1]  */
  135.         dio_pa_bitput (bitnum, 1);      /* set   bit (ON )      */
  136.  
  137.         for ( i = 0; i < 1000; i++ ){   /* simple delay loop    */
  138.             printf ("...\r");
  139.             }
  140.         }
  141.  
  142.     /*  --------------------------------------------------------
  143.      *  Digital Input Demonstration
  144.      *  --------------------------------------------------------
  145.      */
  146.  
  147.     printf ("\n" );
  148.     printf ("----------------------------------------------\n");
  149.     printf ("Digital Input Demonstration.\n");
  150.     printf ("Set up some input circuitry on ANY of the 8255\n");
  151.     printf ("digital I/O lines.  They will be sensed.\n");
  152.     printf ("ALL lines will be set for input so remove any detection\n");
  153.     printf ("(OUTPUT) circuitry at this time, please.\n");
  154.     kb_wait();
  155.  
  156.     for ( j = 0; j < chpcnt; j ++ ){
  157.         dio_pa_config (j, 1, 1, 1, 1);      /* all ports input */
  158.         }
  159.     for ( bitnum = 0; bitnum < bitcnt; bitnum++ ){
  160.         dio_pa_bitget (bitnum, &bstate);    /* all bits read    */
  161.         }
  162.  
  163.     printf ("Read ALL bits in digital IO...\n");
  164.     for ( bitnum = 0; bitnum < bitcnt; bitnum++ ){
  165.         dio_pa_bitget (bitnum, &bstate);    /* bit is read      */
  166.         printf ("Bit %2d is ", bitnum);
  167.         if ( bstate )   printf ("SET  \n");
  168.         else            printf ("CLEAR\n");
  169.         if ( (bitnum+1) % 15 == 0 )         /* so lots o bits doesn't   */
  170.             kb_wait();                      /* scroll off the screen!   */
  171.         }
  172.     kb_wait();
  173.  
  174.     printf ("Set any new input configuration before all bits\n");
  175.     printf ("will be sensed and the settings printed out.\n");
  176.     kb_wait();
  177.  
  178.     for ( bitnum = 0; bitnum < bitcnt; bitnum++ ){
  179.         dio_pa_bitget (bitnum, &bstate);    /* bit is read      */
  180.         printf ("Bit %2d is ", bitnum);
  181.         if ( bstate )   printf ("SET  \n");
  182.         else            printf ("CLEAR\n");
  183.         if ( (bitnum+1) % 15 == 0 )
  184.             kb_wait();
  185.         }
  186.     kb_wait();
  187.  
  188.     printf ("----------------------------------------------\n");
  189.     printf ("That is the end of this demonstration program.\n");
  190.     kb_wait();
  191.  
  192.     exit ( 0 );
  193.     }
  194.  
  195.  
  196. /*- Special Program Exit
  197.  *  Called from anywhere in program.
  198.  *  Allows program to free DIODAT memory
  199.  *  that may have been allocated.
  200.  */
  201. void pg_exit ( short ecode )
  202.     {
  203.     dio_pa_free ();     /* free memory in case allocated    */
  204.     printf ("%s Program Exiting : %d\n", P_NAME, ecode );
  205.     exit (ecode);
  206.     }
  207.  
  208.  
  209. /*-
  210.  *  ----------------------------------------------------------------------
  211.  *  Support Function Definitions
  212.  *  ----------------------------------------------------------------------
  213.  */
  214.  
  215. /*- Flush Keyboard ----------------------------**
  216.  *  Check for keypress, and read them out of
  217.  *  the buffer until there are no more.
  218.  */
  219. void kb_dump (void)
  220.     {
  221.     while ( kbhit() ){              /* key was pressed          */
  222.         if ( (getch() == 0x00) ||   /* read out keypress        */
  223.              (getch() == 0xE0) )
  224.             getch();                /* no function key codes    */
  225.         }
  226.     }
  227.  
  228. /*- Keyboard Wait ----------------------------**
  229.  *  Flush keyboard.
  230.  *  Print blank line, then print a message to press
  231.  *  spacebar to continue, then wait for keypress, then
  232.  *  erase message and return.
  233.  */
  234. void kb_wait (void)
  235.     {
  236.     kb_dump ();
  237.     printf ("\r\n");
  238.     printf ("[ PRESS <SPACE-BAR> TO CONTINUE ]\r");
  239.     if ( getch() == 0 ) getch();
  240.     printf ("                                 \r");
  241.     }
  242.  
  243. /*-
  244.  *  ----------------------------------------------------------------------
  245.  *  END DIOTST02.C Source File
  246.  *  ----------------------------------------------------------------------
  247.  */
  248.